#include "uarch/sim.h"
#include "fakenet.h"
#include "network.h"

extern Simulator Sim;
using namespace manifold::kernel;

Fakenet::~Fakenet() {
}

Fakenet::Fakenet(int num_nodes)
    : Network(num_nodes, 1, 0)
{
    fi_nodes_cid = new CompId_t[num_nodes];
    fi_pointer = new Fakenet_interface*[num_nodes];

    for (int i = 0; i < num_nodes; i++)
    {
        fi_nodes_cid[i] = Component::Create<Fakenet_interface, Network *, ModuleID> (0, this, (ModuleID){i, NI_M});
        fi_pointer[i] = (Fakenet_interface *) Component::GetComponent(fi_nodes_cid[i]);
        Clock::Register<Fakenet_interface> ((Fakenet_interface*)fi_pointer[i], &Fakenet_interface::tick, &Fakenet_interface::tock);
    }
}

Network_interface *Fakenet::get_interface (int id)
{
    return (Network_interface *) fi_pointer[id];
}

CompId_t Fakenet::get_cid (int node_id)
{
    return fi_nodes_cid[node_id];
}

void Fakenet::tick()
{
    for (int i = 0; i < num_connections; i++)
    {
        fi_pointer[i]->tick();
    }
}

void Fakenet::tock ()
{
    for (int i = 0; i < num_connections; i++)
    {
        fi_pointer[i]->tock();
    }
}
